<?php

//This will implement the Course 
//Michael Slaughter 02/02/10

require_once("overhead.php");


class Course{

	var $ID; //Corresponds to the 

	var $number;

	var $department;

	var $description;

	var $name;

//Class Variable Getters


	public function getID(){
		return $this->ID;

	}

	public function getNumber(){
		return $this->number;

	}

	public function getDepartment(){
		return $this->department;

	}

	public function getName(){
		return $this->name;

	}
	public function getDescription() {
		return $this->description;
	}

//Class Variable Setters 

	public function setNumber($number){
		$this->number = $number;

	}

	public function setDepartment($department){
		$this->department = $department;

	}

	public function setDescription($description){
		$this->description = $description;

	}
	public function setName($n) { 
		$this->name=$n;
		}

	
//Class functions
//Get Department info from the database for a department where Department.ID = $id 
	public function retrieveFromID($id){ 
		$queryString = "select ID, Number, DepartmentID, Description, Name from Course where Course.ID=".$id;
		$query = mysql_query($queryString);
		$result = mysql_fetch_assoc($query);
		if ($result){
			//If something was found then put all the fields into the instance variables for this function
			$this->ID = $result['ID'];
			$this->number = $result['Number'];
			$dept= new Department();
			$dept->retrieveFromID($result['DepartmentID']);
			$this->department=$dept;
			$this->description = $result['Description'];
			$this->name = $result['Name'];			
			return true;
		}
		else{
			//It wasn't found or there was an error
			return false;
		}
	}
	public function update(){ //Update the contents of the database to reflect any changes made to this object.
		$dept= $this -> department;
		$queryString = "update Course set Number='".$this->number."',DepartmentID='".$dept->getID()."',Description='".$this->description."',Name='".$this->name."' where Course.ID =".$this->ID;
		$query=mysql_query($queryString);
		if ($queryString){
			return true; //Ensure update worked correctly
		}
		else{
			return false; //Update failed!
		}
	}

	public function insert(){//Takes all of the values for the instance variables and creates a new entry into the table
		$queryString ="insert into Course (Number,DepartmentID,Description,Name) values ('".$this->number."','".$this->department->getID()."','".$this->description."','".$this->name."')";
		if (mysql_query($queryString)){ //if the insert was successful, this will not be null or false.
			//We have to update this new ID to the one the database selected
			$getIDQueryString="select LAST_INSERT_ID() as NewID";
			$getIDQuery=mysql_query($getIDQueryString);
			$result=mysql_fetch_assoc($getIDQuery);
			if($result) { //If successful set the new ID to be that latest entry
				$this->ID=$result['NewID'];
				return true;
			}
			else{
				return false;
			}
		}
		else{ 
			return false;
		}
	}
	public function delete(){ //Deletes the Row of the Course that has the corresponding ID
		$queryString = "delete from Course where Course.ID =".$this->ID;
		if(mysql_query($queryString)){
			return true;
		}
		else{
			return false;
		}
	}
	public function getLatest(){
		$queryString="select ID, Number, DepartmentID, Description, Name from Course where Course.ID =".$this->ID;
		$query=mysql_query($queryString);
		$result=mysql_fetch_assoc($query);
			if ($result) {
				//if something was found, put all the fields from the database (in $result right now) into
				//the instance variables for this object
				$this->ID = $result['ID'];
				$this->number = $result['Number'];
				$dept=new Department();
				$dept->retrieveFromID($result['DepartmentID']);
				$this->department=$dept;
				$this->description = $result['Description'];
				$this->name = $result['Name'];
				//return true because we found it!
				return true;
			}
			else{
				return false;
			}
	}
	

}


?>